﻿using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Swashbuckle.AspNetCore.Swagger;
using UEditor.Core;

namespace LmgCMS.Areas.API.Controllers
{
    /// <summary>
    /// 文件上传
    /// </summary>
    [ApiExplorerSettings(GroupName = SwaggerGroupingModel.GROUPNAME_COMM)]
    public class FileController : BaseController
    {
        private readonly string _filePath;
        private readonly UEditorService _ueditorService;
        private readonly IWebHostEnvironment _env;
        public FileController(UEditorService ueditorService, IWebHostEnvironment env)
        {
            this._ueditorService = ueditorService;
            this._env = env;
            _filePath = AppConfig.UploadPath;
            if (string.IsNullOrEmpty(_filePath))
            {
                _filePath = Path.Combine(Directory.GetCurrentDirectory(), "upfile"); 
            }
        }
        /// <summary>
        ///  批量上传文件接口
        /// </summary>
        /// <param name="files"></param>
        /// <returns>服务器存储的文件信息</returns>
        [HttpPost]
        public IActionResult Upload(IFormFileCollection files)
        {
            List<string> result = new List<string>();
            string folder = DateTime.Now.ToString("yyyyMMdd");
            try
            {
                foreach (var file in files)
                {
                    if (file != null && file.Length > 0 && file.Length < 10485760)
                    {
                        using (var binaryReader = new BinaryReader(file.OpenReadStream()))
                        {
                            string uuid = UUIDUtils.Fid;
                            var fileName = Path.GetFileName(file.FileName);
                            var fileBuffers = binaryReader.ReadBytes((int)file.Length);

                            var uploadPath = Path.Combine(_filePath, folder);
                            if (!Directory.Exists(uploadPath))
                            {
                                Directory.CreateDirectory(uploadPath);
                            }
                            var ext = Path.GetExtension(fileName).ToLower();
                            string newName = uuid + ext;
                            string savePath = Path.Combine(uploadPath, newName);
                            using (var fs = new FileStream(savePath, FileMode.Create))
                            {
                                fs.Write(fileBuffers, 0, fileBuffers.Length);
                                fs.Close();
                                //生成缩略图
                                //if (ext.Contains(".jpg") || ext.Contains(".jpeg") || ext.Contains(".png") || ext.Contains(".bmp") || ext.Contains(".gif"))
                                //{
                                //    string thumbnailName = uuid + "_thumbnai" + ext;
                                //    ImgHelper.MakeThumbnail(Path.Combine(uploadPath, newName), Path.Combine(uploadPath, thumbnailName));
                                //    string _dbThumbnail = Path.Combine(folder, thumbnailName);

                                //    result.Add(_dbThumbnail);
                                //}
                                //else
                                {
                                    string _dbFilePath = Path.Combine(folder, newName);
                                    result.Add(_dbFilePath);
                                }
                            }

                        }
                    }
                }
            }
            catch (Exception ex)
            {

            }
            return Content(result.ToJson());
        }
        /// <summary>
        ///  UEditor 上传文件接口
        /// </summary>
        /// <returns></returns>
        [HttpGet, HttpPost]
        public IActionResult UeditorUploadFile()
        {
            var response = _ueditorService.UploadAndGetResponse(HttpContext);
            return Content(response.Result, response.ContentType);
        }
    }
}
